<?php
// +----------------------------------------------------------------------
// | my
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\Printer\model;

use think\Model;

class Printer extends Model
{

    protected $table = "my_printer";
    protected $autoWriteTimestamp = true;

    /**
     * 获取打印机，并且缓存
     */
    public function info($printer_id){

      $options = $this->where('id',$appid)-> cache(true,60)->find();
      
      return $options;
    }

    public function infoByMap($map,$field='*'){

      $printers = $this->field($field)->where($map)-> cache(true,60)->find();
      
      return $options;
    }

    public function listByMap($map,$field='*'){

      
      $map['status']=['EGT',0];
      $printers = $this->field($field)->where($map)-> cache(true,60)->select();
      
      return $printers;
    }

    public function addTotal($id){

      $this->where('id',$id)->setInc('total');
      $this->where('id',$id)->setField('update_time',time());
      
      
    }

    /**
     * 检查符合要求的打印机
     * 
     */
    public function checkPrinter($appid,$openid){

        $printers=$this->where('appid',$appid)->select();  
        if (!$printers) return false;
        if (count($printers)==1) return  $printers[0];
        if (count($printers)>1) $scan=model('we/WeSceneScan')->getLastScan($openid);  //获取这个用户最后扫描的二维码
        foreach ($printers as $key => $printer) {
        	if ($scan['shopid']==$printer['shopid']) return $printer;
        }
        
    }

     public function editData(){

      $data=input("post.");
      $data['aid']=session('aid');
      if(isset($data['file'])) unset($data['file']);
      
      if ($data['id']){
         $res = $this->allowField(true)->where('id',$data['id'])->update($data);
      }else{
         $res = $this->allowField(true)->data($data)->save();
      }
      
      return $res;
    }


     public function weOrder($data) {

         $printers=db('printer')->where('aid',$data['aid'])->select();
          
         $message='打印机诊断工具';
         $online='';
 
       
         foreach ($printers as $key => &$printer) {
            if (cache('printer_online_'.$printer['shopid'])==1){
             $online='-在线/:rose';
            }else{
              $online="-离线/:fade"; 
              $shopid=$printer['shopid'];
            }
            $message.="\n".$printer['title'].$online;
         }

         if (isset($shopid)){
           $shop=model('admin/Shop')->info($printer['shopid'],'mobile');
           $message.="\n请在打印机电脑打开".$_SERVER['HTTP_HOST']."/printer/?shopid=".$printer['shopid']."&m=".$shop['mobile']; 
         }

        
         $message.="\n-------------------";
         $message.="\n神奇的大写字母P：\n（P:我是您的打印助手，打印机Printer的首字母）";
         $message.="\n/:hug1.打印充值：\n12P1 (给12号粉丝充值1张)";
         $message.="\n/:hug2.查询粉丝信息：\n12P (查询12号粉丝信息)";
         $message.="\n/:hug3.打印列表：\n列表P";
         $message.="\n/:hug3.再次打印：\n再打P照片编号";
         return ['message'=>$message];
     }


    

    
}